<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/img/icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/img/icon.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="ACM/ICPC,二分,RMQ,Hash,后缀数组,uva," />










<meta name="description" content="解法一：后缀数组+RMQ首先将$n$个字符串连在一起，中间用大于$’z’$的$n-1$个互不相同的字符(实际要用int，不然会爆char的范围)隔开。然后根据后缀数组height的定义，可以知道，假设$p{a}$代表$a$字符串首字母的位置，同理$p{b}$代表$b$字符串首字母的位置，此处不妨设$rank{p{a}} &amp;lt; rank{p{b}}$。然后只需要求出$rank{p{a}} + 1">
<meta name="keywords" content="ACM&#x2F;ICPC,二分,RMQ,Hash,后缀数组,uva">
<meta property="og:type" content="article">
<meta property="og:title" content="uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分)">
<meta property="og:url" content="http://localhost/uva12338/index.html">
<meta property="og:site_name" content="cayun&#39;s blog">
<meta property="og:description" content="解法一：后缀数组+RMQ首先将$n$个字符串连在一起，中间用大于$’z’$的$n-1$个互不相同的字符(实际要用int，不然会爆char的范围)隔开。然后根据后缀数组height的定义，可以知道，假设$p{a}$代表$a$字符串首字母的位置，同理$p{b}$代表$b$字符串首字母的位置，此处不妨设$rank{p{a}} &amp;lt; rank{p{b}}$。然后只需要求出$rank{p{a}} + 1">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-08-07T08:18:05.510Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分)">
<meta name="twitter:description" content="解法一：后缀数组+RMQ首先将$n$个字符串连在一起，中间用大于$’z’$的$n-1$个互不相同的字符(实际要用int，不然会爆char的范围)隔开。然后根据后缀数组height的定义，可以知道，假设$p{a}$代表$a$字符串首字母的位置，同理$p{b}$代表$b$字符串首字母的位置，此处不妨设$rank{p{a}} &amp;lt; rank{p{b}}$。然后只需要求出$rank{p{a}} + 1">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://localhost/uva12338/"/>





  <title>uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分) | cayun's blog</title>
  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-93052608-1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1ca7c9c8011b37b0c209724dc77c24db";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">cayun's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">不积跬步, 无以至千里!</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/uva12338/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分)</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-16T00:00:00+08:00">
                2017-07-16
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/uva12338/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="uva12338/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <h1 id="解法一：后缀数组-RMQ"><a href="#解法一：后缀数组-RMQ" class="headerlink" title="解法一：后缀数组+RMQ"></a>解法一：后缀数组+RMQ</h1><p>首先将$n$个字符串连在一起，中间用大于$’z’$的$n-1$个互不相同的字符(实际要用int，不然会爆char的范围)隔开。然后根据后缀数组height的定义，可以知道，假设$p<em>{a}$代表$a$字符串首字母的位置，同理$p</em>{b}$代表$b$字符串首字母的位置，此处不妨设$rank<em>{p</em>{a}} &lt; rank<em>{p</em>{b}}$。然后只需要求出$rank<em>{p</em>{a}} + 1$到$rank<em>{p</em>{b}}$之间的最小height值，即为$a$和$b$的最长公共前缀。对于$a=b$的情况特别判断一下即可。</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1100010</span>;</div><div class="line"></div><div class="line"><span class="keyword">int</span> sa[N * <span class="number">3</span>], rnk[N], height[N], r[N];</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb))</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> G(x) ((x) &lt; tb ? (x) * 3 + 1 : ((x) - tb) * 3 + 2)</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">SuffixArray</span> &#123;</span></div><div class="line">    <span class="keyword">enum</span> &#123;MAXN = <span class="number">1100010</span>&#125;;</div><div class="line">    <span class="keyword">int</span> wa[MAXN * <span class="number">3</span>], wb[MAXN * <span class="number">3</span>], wv[MAXN * <span class="number">3</span>], ws[MAXN * <span class="number">3</span>];</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">c0</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> r[a] == r[b] &amp;&amp; r[a+<span class="number">1</span>] == r[b+<span class="number">1</span>] &amp;&amp; r[a+<span class="number">2</span>] == r[b+<span class="number">2</span>];</div><div class="line">    &#125; </div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">c12</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span> *r, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (k == <span class="number">2</span>) <span class="keyword">return</span> r[a] &lt; r[b] || r[a] == r[b] &amp;&amp; c12(<span class="number">1</span>, r, a + <span class="number">1</span>, b + <span class="number">1</span>);</div><div class="line">        <span class="keyword">else</span> <span class="keyword">return</span> r[a] &lt; r[b] || r[a] == r[b] &amp;&amp; wv[a+<span class="number">1</span>] &lt; wv[b+<span class="number">1</span>];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sort</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *a, <span class="keyword">int</span> *b, <span class="keyword">int</span> n, <span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) wv[i] = r[a[i]];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) ws[i] = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) ws[wv[i]]++;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; m; i++) ws[i] += ws[i<span class="number">-1</span>];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) b[--ws[wv[i]]] = a[i];</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dc3</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> n, <span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> i, j, *rn = r + n, *san = sa + n, ta = <span class="number">0</span>, tb = (n + <span class="number">1</span>) / <span class="number">3</span>, tbc = <span class="number">0</span>, p;</div><div class="line">        r[n] = r[n+<span class="number">1</span>] = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (i % <span class="number">3</span> != <span class="number">0</span>) wa[tbc++] = i;</div><div class="line">        &#125;</div><div class="line">        sort(r + <span class="number">2</span>, wa, wb, tbc, m);</div><div class="line">        sort(r + <span class="number">1</span>, wb, wa, tbc, m);</div><div class="line">        sort(r, wa, wb, tbc, m);</div><div class="line">        <span class="keyword">for</span> (p = <span class="number">1</span>, rn[F(wb[<span class="number">0</span>])] = <span class="number">0</span>, i = <span class="number">1</span>; i &lt; tbc; i++)</div><div class="line">            rn[F(wb[i])] = c0(r, wb[i<span class="number">-1</span>], wb[i]) ? p - <span class="number">1</span> : p++;</div><div class="line">        <span class="keyword">if</span> (p &lt; tbc) dc3(rn, san, tbc, p);</div><div class="line">        <span class="keyword">else</span> &#123;</div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) san[rn[i]] = i;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (san[i] &lt; tb) wb[ta++] = san[i] * <span class="number">3</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (n % <span class="number">3</span> == <span class="number">1</span>) wb[ta++] = n - <span class="number">1</span>;</div><div class="line">        sort(r, wb, wa, ta, m);</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; tbc; i++) wv[wb[i] = G(san[i])] = i;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>, j = <span class="number">0</span>, p = <span class="number">0</span>; i &lt; ta &amp;&amp; j &lt; tbc; p++)</div><div class="line">            sa[p] = c12(wb[j] % <span class="number">3</span>, r, wa[i], wb[j]) ? wa[i++] : wb[j++];</div><div class="line">        <span class="keyword">for</span> (; i &lt; ta; p++) sa[p] = wa[i++];</div><div class="line">        <span class="keyword">for</span> (; j &lt; tbc; p++) sa[p] = wb[j++]; </div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">getHeight</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> *rnk, <span class="keyword">int</span> *height, <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> i, j, k = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++) rnk[sa[i]] = i;</div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; height[rnk[i++]] = k) &#123;</div><div class="line">            <span class="keyword">for</span> (k ? k-- : <span class="number">0</span>, j = sa[rnk[i] - <span class="number">1</span>]; r[i+k] == r[j+k]; k++);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;suffixArray;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> ST_SIZE = <span class="number">22</span>;</div><div class="line"><span class="keyword">char</span> s[N];</div><div class="line"><span class="keyword">int</span> p[N], len[N];</div><div class="line"><span class="keyword">int</span> st[N][ST_SIZE];</div><div class="line"></div><div class="line"><span class="comment">//a数组从0开始计数</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">initST</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> n)</span> </span>&#123;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)</div><div class="line">        st[i][<span class="number">0</span>] = a[i];</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; ST_SIZE; j++) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i + (<span class="number">1</span> &lt;&lt; j) &lt;= n; i++)</div><div class="line">            st[i][j] = min(st[i][j - <span class="number">1</span>], st[i + (<span class="number">1</span> &lt;&lt; (j - <span class="number">1</span>))][j - <span class="number">1</span>]);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">queryST</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> k = (<span class="keyword">int</span>)log2(r + <span class="number">1</span> - l + <span class="number">0.000001</span>);</div><div class="line">    <span class="keyword">return</span> min(st[l][k], st[r + <span class="number">1</span> - (<span class="number">1</span> &lt;&lt; k)][k]);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++) &#123;</div><div class="line">        <span class="keyword">int</span> n; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</div><div class="line">        <span class="keyword">int</span> m = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s);</div><div class="line">            p[i] = m; len[i] = <span class="built_in">strlen</span>(s);</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; s[j] != <span class="number">0</span>; j++) r[m++] = s[j] - <span class="string">'a'</span> + <span class="number">1</span>;</div><div class="line">            r[m++] = i == n ? <span class="number">0</span> : i + <span class="number">26</span>;</div><div class="line">        &#125;</div><div class="line">        m--;</div><div class="line">        suffixArray.dc3(r, sa, m + <span class="number">1</span>, n + <span class="number">30</span>);</div><div class="line">        suffixArray.getHeight(r, sa, rnk, height, m);</div><div class="line"></div><div class="line">        initST(height, m + <span class="number">1</span>);</div><div class="line"></div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case %d:\n"</span>, kk);</div><div class="line">        <span class="keyword">int</span> q; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;q);</div><div class="line">        <span class="keyword">while</span> (q--) &#123;</div><div class="line">            <span class="keyword">int</span> a, b; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;a, &amp;b);</div><div class="line">            <span class="keyword">if</span> (a == b) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, len[a]);</div><div class="line">            <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, queryST(min(rnk[p[a]], rnk[p[b]]) + <span class="number">1</span>, max(rnk[p[a]], rnk[p[b]])));</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="解法二：Hash-二分"><a href="#解法二：Hash-二分" class="headerlink" title="解法二：Hash+二分"></a>解法二：Hash+二分</h1><p>对于每个字符串前缀进行Hash处理一下，然后对于每组询问，二分长度进行求解。</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> SEED1 = <span class="number">179</span>;</div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> SEED2 = <span class="number">239</span>;</div><div class="line"><span class="built_in">string</span> s[N];</div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; hash1[N], hash2[N];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (hash1[a][l] == hash1[b][l] &amp;&amp; hash2[a][l] == hash2[b][l]) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= l; i++) &#123;</div><div class="line">            <span class="keyword">if</span> (s[a][i] != s[b][i]) <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="built_in">std</span>::ios::sync_with_stdio(<span class="literal">false</span>);</div><div class="line">    <span class="built_in">std</span>::<span class="built_in">cin</span>.tie(<span class="number">0</span>);</div><div class="line"></div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">cin</span> &gt;&gt; T;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++) &#123;</div><div class="line">        <span class="keyword">int</span> n; <span class="built_in">cin</span> &gt;&gt; n;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;</div><div class="line">            hash1[i].clear(); hash2[i].clear();</div><div class="line">            <span class="built_in">cin</span> &gt;&gt; s[i];</div><div class="line">            <span class="keyword">int</span> h1 = <span class="number">0</span>, h2 = <span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; s[i].size(); j++) &#123;</div><div class="line">                h1 = h1 * SEED1 + s[i][j];</div><div class="line">                h2 = h2 * SEED2 + s[i][j];</div><div class="line">                hash1[i].push_back(h1);</div><div class="line">                hash2[i].push_back(h2);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Case "</span> &lt;&lt; kk &lt;&lt; <span class="string">":\n"</span>;</div><div class="line">        <span class="keyword">int</span> q; <span class="built_in">cin</span> &gt;&gt; q;</div><div class="line">        <span class="keyword">while</span> (q--) &#123;</div><div class="line">            <span class="keyword">int</span> a, b; <span class="built_in">cin</span> &gt;&gt; a &gt;&gt; b;</div><div class="line">            <span class="keyword">int</span> ans = <span class="number">-1</span>;</div><div class="line">            <span class="keyword">int</span> l = <span class="number">0</span>, r = min((<span class="keyword">int</span>)hash1[a].size(), (<span class="keyword">int</span>)hash2[a].size()) - <span class="number">1</span>;</div><div class="line">            <span class="keyword">while</span> (l &lt;= r) &#123;</div><div class="line">                <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;</div><div class="line">                <span class="keyword">if</span> (judge(mid, a, b)) &#123;</div><div class="line">                    ans = mid; l = mid + <span class="number">1</span>;</div><div class="line">                &#125; <span class="keyword">else</span> &#123;</div><div class="line">                    r = mid - <span class="number">1</span>;</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; ans + <span class="number">1</span> &lt;&lt; <span class="string">"\n"</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    cayun
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://localhost/uva12338/" title="uva 12338 Anti-Rhyme Pairs (后缀数组+RMQ || Hash+二分)">http://localhost/uva12338/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/ACM-ICPC/" rel="tag"># ACM/ICPC</a>
          
            <a href="/tags/二分/" rel="tag"># 二分</a>
          
            <a href="/tags/RMQ/" rel="tag"># RMQ</a>
          
            <a href="/tags/Hash/" rel="tag"># Hash</a>
          
            <a href="/tags/后缀数组/" rel="tag"># 后缀数组</a>
          
            <a href="/tags/uva/" rel="tag"># uva</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/gym101158E/" rel="next" title="Gym 101158E Infallibly Crack Perplexing Cryptarithm (模拟)">
                <i class="fa fa-chevron-left"></i> Gym 101158E Infallibly Crack Perplexing Cryptarithm (模拟)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/codeforces24D/" rel="prev" title="codeforces 24D Broken robot (概率DP+三对角矩阵)">
                codeforces 24D Broken robot (概率DP+三对角矩阵) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/img/head.png"
                alt="cayun" />
            
              <p class="site-author-name" itemprop="name">cayun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">102</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/comeacrossyun" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:comeacrossyun@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#解法一：后缀数组-RMQ"><span class="nav-text">解法一：后缀数组+RMQ</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#解法二：Hash-二分"><span class="nav-text">解法二：Hash+二分</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">cayun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a></div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cyt6VLWhj';
      var conf = 'prod_e821ad3bd562047515ebc870fa370e7e';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  









  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
